package com.xhui.vimarket.zkplus.zul.attr.evt;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zul.Button;
import org.zkoss.zul.Div;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.xhui.crm.common.Constants;
import com.xhui.crm.common.util.crm.WeakPassword;
import com.xhui.crm.common.util.numeric.NumericUtil;
import com.xhui.crm.common.util.string.StrUtil;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.ui.id.ZKRebcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zul.attr.ada.Atcom.xhui.vimarket.zkplustf;
import com.xhui.vimarket.zkplus.zul.com.xhui.vimarket.zkplusttrBaseComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrPasswordTwoComp;

/**
 * .
 * 
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2011-5-5
 * @功能说明：
 * 
 */
public class AttrPasswordTwoComposer extends AttrBaseComposer implements AttrPasswordTwoIntf {
    
    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 6326094864263772273L;
    
    private Window win;
    
    @Override
    public ComponentInfo doBeforeCompose(final Page page, final Component comp,
        final ComponentInfo info) {
        return info;
    }
    
    @Override
    public void doAfterCompose(final Component comp) throws Exception {
        super.doAfterCompose(comp);
    }
    
    /**
     * onCreate.
     */
    public void onCreate() {
    }
    
    /**
     * 验证.
     * @return RetVO
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    public RetVO validate() {
        Component root = this.self.getParent();
        String oldVal = AttrPasswordTwoComp.getInitValue(root);
        String newVal = this.getTextbox().getValue() + "";
        String newValTwo = this.getTextboxTwo().getValue() + "";
        if (!newVal.equals(newValTwo)) { // 两次密码不一致
            return RetVO.newInstance(Constants.RET_FALSE, "两次密码输入不一致，请重新设置");
        } else if (!"".equals(newVal) && (oldVal == null || !oldVal.equals(newVal))) { // 有设置，并且修改了值(旧值为空，或者新旧值不同)
            if (WeakPassword.isWeak(newVal)) { // 弱密码限制
                return RetVO.newInstance(Constants.RET_FALSE, "密码设置过于简单，请重新设置");
            }
        }
        return null;
    }
    
    /**
     * 获取值.
     * 不加密的值
     * 
     * @return String
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    public String getValue() {
        RetVO valiVo = this.validate();
        if (valiVo != null && Constants.RET_FALSE.equals(valiVo.getResult())) {
            throw new WrongValueException(this.getTextbox(), valiVo.getRetMsg());
        }
        String newVal = this.getTextbox().getValue() + "";
        return newVal;
    }
    
    /**
     * 获取原始值.
     * 原始的值
     * 
     * @return String
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    public String getRealValue() {
        String newVal = this.getTextbox().getValue() + "";
        return newVal;
    }
    
    /**
     * .
     * 
     * @param event Event
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    public void onInitVal(Event event) {
        this.doInitVal();
    }
    
    /**
     * onTextboxFocus.
     * 
     * @param event Event
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    public void onTextboxFocus(Event event) {
        if (event != null) {
            ForwardEvent fevt = (ForwardEvent) event;
            Component oriComp = fevt.getOrigin().getTarget();
            if (oriComp != null && oriComp instanceof Textbox) {
                Textbox tb = (Textbox) oriComp;
                if (!StrUtil.isNullOrEmpty(tb.getValue())) {
                    tb.setSelectedText(0, tb.getValue().length(), tb.getValue(), true);
                }
            }
        }
    }
    
    /**
     * onTextboxFocus.
     * 
     * @param event Event
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    public void onTextboxBlur(Event event) {
        Events.postEvent(Events.ON_CHANGE, AttrPasswordTwoComposer.this.getTextboxTwo(), null);
    }
    
    /**
     * doInitVal.
     * 
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    protected void doInitVal() {
        final String ival = NumericUtil.getRandomCode(6, false);
    
        Object obj = this.getTextbox().getParent().getParent().getAttribute(AttrBaseComp.COMP_ARG);
        if (obj != null) {
            this.showInitPwd(ival);
        } else {
            this.getTextbox().setValue(ival);
            this.getTextboxTwo().setValue(ival);
            Events.postEvent(Events.ON_CHANGE, AttrPasswordTwoComposer.this.getTextboxTwo(), null);
        }
    }
    /**
     * 
     * 初始化随机密码显示.
     * @param ival 初始化随机密码
     * @author luxb
     * 		   2012-5-24 luxb
     */
    public void showInitPwd(final String ival) {
        win = new Window();
        win.setTitle("统一密码初始化");
        win.setWidth("195px");
        win.setHeight("90px");
        Div div = new Div();
        Div d1 = new Div();
        div.setStyle("margin:10px");
        Hbox sbox = new Hbox();
        Label l1 = new Label("密码:");
        Label l2 = new Label(ival);
        l1.setParent(sbox);
        l2.setParent(sbox);
        sbox.setParent(d1);
        
        Div di = new Div();
        di.setStyle("text-align:center;margin-top:10px;");
        Button btn = new Button("确定");
        String attrId=this.getTextbox().getParent().getParent().getAttribute(AttrBaseComp.COMP_ATTRID) +"";
        //重构Uuid用于压力测试。
        ZKRebuildUuid.rebuildUuidById(btn, "btnPsw_"+ attrId);
        Button cbtn = new Button("取消");
        
        //重构Uuid用于压力测试。
        ZKRebuildUuid.rebuildUuidById(cbtn, "cbtnPsw_"+attrId);
        cbtn.setStyle("margin-left:10px");
        btn.setParent(di);
        cbtn.setParent(di);
        d1.setParent(div);
        di.setParent(div);
        div.setParent(win);
        win.setParent(this.getTextbox().getParent());
        win.setPosition("center,center");
        win.doPopup();
        btn.addEventListener(Events.ON_CLICK, new EventListener() {
            @Override
            public void onEvent(Event event) throws Exception {
                win.detach();
                AttrPasswordTwoComposer.this.getTextbox().setValue(ival);
                AttrPasswordTwoComposer.this.getTextboxTwo().setValue(ival);
                Events.postEvent(Events.ON_CHANGE, AttrPasswordTwoComposer.this.getTextboxTwo(), null);
            }
        });
        
        cbtn.addEventListener(Events.ON_CLICK, new EventListener() {
            @Override
            public void onEvent(Event event) throws Exception {
                win.detach();
            }
        });
    }
    
    /**
     * .
     * 
     * @return Textbox
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    protected Textbox getTextbox() {
        return AttrPasswordTwoComp.getTextbox(this.self.getParent());
    }
    
    /**
     * .
     * 
     * @return Textbox
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    protected Textbox getTextboxTwo() {
        return AttrPasswordTwoComp.getTextboxTwo(this.self.getParent());
    }
    
    /**
     * onResetTetxboxTwo.
     * 
     * @param event Event
     * @author wuyx
     * 2011-6-3 wuyx 
     */
    public void onResetTetxboxTwo(Event event) {
        // 如果输入框1和输入框2值不同，清空输入框2的值
        String ts1 = this.getTextbox().getValue();
        Textbox tb2 = this.getTextboxTwo();
        String ts2 = tb2.getValue();
        if (!StrUtil.strnull(ts1).equals(ts2)) {
            tb2.setValue("");
        }
    }
    
}
